package linwen.wang.validated;

import linwen.wang.annoation.Phone;
import org.springframework.util.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * @ClassName PhoneValidator
 * @Description TODO
 * @Author zero
 * @DATE 2023/8/3 11:23 PM
 * @Version 1.0
 */
public class PhoneValidator implements ConstraintValidator<Phone, String> {

    /**
     * @Author zero
     * @Description //返回true 标识校验通过，false 校验失败
     * @Date 11:34 PM 2023/8/3
     * @Param
     * @return
     **/
    @Override
    public boolean isValid(String phone, ConstraintValidatorContext constraintValidatorContext) {
        if (StringUtils.isEmpty(phone)) {
            return false;
        }
        //获取默认提示信息
        String defaultConstraintMessageTemplate = constraintValidatorContext.getDefaultConstraintMessageTemplate();
        System.out.println("default message :" + defaultConstraintMessageTemplate);
        //禁用默认提示信息
        constraintValidatorContext.disableDefaultConstraintViolation();
        //设置提示语
        constraintValidatorContext.buildConstraintViolationWithTemplate("手机号格式错误").addConstraintViolation();
        String regex = "^1(3|4|5|7|8)\\d{9}$";
        return phone.matches(regex);
    }

    @Override
    public void initialize(Phone constraintAnnotation) {

    }

}